package org.qphoto;

import java.util.List;

import org.qphoto.SendAdapter.ViewHolder;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class UserMessageArrayAdapter extends ArrayAdapter<Message>{

	private List<Message> list = null;
	private Activity context   = null;
	private OnItemChildCreateListener onItemChildCreateListener = null;
	
	public OnItemChildCreateListener getOnItemChildCreateListener() {
		return onItemChildCreateListener;
	}

	public void setOnItemChildCreateListener(
			OnItemChildCreateListener onItemChildCreateListener) {
		this.onItemChildCreateListener = onItemChildCreateListener;
	}

	public UserMessageArrayAdapter(Activity context, List<Message> list) {
		super(context, 0, list);
		
		this.context = context;
		this.list    = list;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view  = null;
		ViewHolder holder = null;  
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.usermessageitem, null);

			holder = new ViewHolder();     
			holder.contentTextView   = (TextView) view
					.findViewById(R.id.msgcontent);
			holder.picImageView     = (ImageView) view
					.findViewById(R.id.msgimage);
			holder.posttimeTextView = (TextView) view
					.findViewById(R.id.posttime);
			view.setTag(holder);
		} else {
			view = convertView;
			holder = (ViewHolder) view.getTag();    
		}
		
		// get message
		Message message = list.get(position);
		if ( message.getContent().equals("") == true)
		{
			holder.contentTextView.setText("...");
		} else {
			holder.contentTextView.setText(message.getContent());
		}
		holder.posttimeTextView.setText(message.getShowPostTime());
		
		// set image
		holder.picImageView.setVisibility(View.VISIBLE);
		if ( message.getImageuri().equals("") == false)
		{
			Bitmap picimage  = BitmapFactory.decodeFile(message.getImageuri());
			if ( picimage != null)
			{
				int bmpWidth     = picimage.getWidth();
				int bmpHeight    = picimage.getHeight();
				holder.picImageView.getLayoutParams().width = 120;
				int vWidth = holder.picImageView.getLayoutParams().width;
				if (bmpWidth < vWidth) {
					holder.picImageView.setScaleType(ScaleType.CENTER);
					holder.picImageView.getLayoutParams().height = bmpHeight;
				} else {
					holder.picImageView.setScaleType(ScaleType.FIT_CENTER);
					holder.picImageView.getLayoutParams().height = bmpHeight * vWidth
							/ bmpWidth;
				}
				holder.picImageView.setImageBitmap(picimage);
			} else {
				holder.picImageView.setId(R.drawable.default_image);
				holder.picImageView.getLayoutParams().height = 10;
				holder.picImageView.setVisibility(View.INVISIBLE);
			}
		} else if ( message.getAudiouri().equals("") == false )
		{
			holder.picImageView.setImageResource(R.drawable.default_audioimage);
			holder.picImageView.getLayoutParams().height = 53;
			holder.picImageView.getLayoutParams().width  = 156;
		} else if(message.getVideouri().equals("") == false)
		{
			holder.picImageView.setImageResource(R.drawable.default_videoimage);
			holder.picImageView.getLayoutParams().height = 53;
			holder.picImageView.getLayoutParams().width  = 156;
		} else {
			holder.picImageView.setImageResource(R.drawable.default_image);
			holder.picImageView.getLayoutParams().height = 10;
			holder.picImageView.setVisibility(View.INVISIBLE);
		}
		
		// listener
		if ( this.onItemChildCreateListener != null)
		{
			this.onItemChildCreateListener.OnCreateChildView(view, position, holder.picImageView, R.id.msgimage);
		}

		return view;
	}
	
	public final class ViewHolder {    
		public ImageView picImageView;
		public TextView  contentTextView;         
		public TextView  posttimeTextView;       
	}   
}
